home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / ddeexamp.zip / EXECFNS.C < prev    next >
C/C++ Source or Header  |  1993-07-08  |  8KB  |  383 lines

  1. /*
  2.     This module contains all the DDE Execute functions
  3.     supported by this server.
  4.  
  5.     Drawing functions are:
  6.  
  7.     Pen(RED|BLUE|GREEN|BLACK|WHITE|YELLOW|CYAN|MAGENTA)
  8.     Brush(RED|BLUE|GREEN|BLACK|WHITE|YELLOW|CYAN|MAGENTA)
  9.     Line(left,top,right,bottom)
  10.     Ellipse(left,top,right,bottom)
  11.     Rectangle(left,top,right,bottom)
  12.     Erase()
  13.     Text(x,y,text)
  14.  
  15. */
  16.  
  17. #include "ddeexec.h"
  18.  
  19. //
  20. // local data 
  21. //
  22.  
  23. static HPEN hpenCurrent;
  24. static HBRUSH hbrCurrent;
  25. static HPEN hpenOld;
  26. static HBRUSH hbrOld;
  27. static COLORREF rgbBrush;
  28. static COLORREF rgbPen;
  29.  
  30. //
  31. // local functions
  32. //
  33.  
  34. static BOOL GetRectangle(LPSTR pszError, UINT uiErrorSize, UINT uiNargs, LPSTR FAR *ppArgs, LPRECT pRect);
  35. static BOOL GetColor(LPSTR pszError, UINT uiErrorSize, UINT uiNargs, LPSTR FAR *ppArgs, COLORREF *pColor);
  36. static void SetTools(HDC hDC);
  37. static void RestoreTools(HDC hDC);
  38. static void SetPenColor(COLORREF rgb);
  39. static void SetBrushColor(COLORREF rgb);
  40.  
  41. //
  42. // Pen(color)
  43. //
  44.  
  45. BOOL FAR PenFn(PDDETOPICINFO pTopic,
  46.                  LPSTR pszResult,
  47.                  UINT uiResultSize,
  48.                  UINT uiNargs,
  49.                  LPSTR FAR *ppArgs)
  50. {
  51.     COLORREF color;
  52.  
  53.     Status("Pen: %s", ppArgs[0]);
  54.  
  55.     if (!GetColor(pszResult, uiResultSize, uiNargs, ppArgs, &color)) {
  56.         return FALSE;
  57.     }
  58.  
  59.     SetPenColor(color);
  60.  
  61.     return TRUE;
  62. }
  63.  
  64. //
  65. // Brush(color)
  66. //
  67.  
  68. BOOL FAR BrushFn(PDDETOPICINFO pTopic,
  69.                  LPSTR pszResult,
  70.                  UINT uiResultSize,
  71.                  UINT uiNargs,
  72.                  LPSTR FAR *ppArgs)
  73. {
  74.     COLORREF color;
  75.  
  76.     Status("Brush: %s", ppArgs[0]);
  77.  
  78.     if (!GetColor(pszResult, uiResultSize, uiNargs, ppArgs, &color)) {
  79.         return FALSE;
  80.     }
  81.  
  82.     SetBrushColor(color);
  83.  
  84.     return TRUE;
  85. }
  86.  
  87. //
  88. // Ellipse(left,top,right,bottom)
  89. //
  90.  
  91. BOOL FAR EllipseFn(PDDETOPICINFO pTopic,
  92.                  LPSTR pszResult,
  93.                  UINT uiResultSize,
  94.                  UINT uiNargs,
  95.                  LPSTR FAR *ppArgs)
  96. {
  97.     HDC hDC;
  98.     RECT rc;
  99.  
  100.     //
  101.     // Get the co-ordinates
  102.     //
  103.  
  104.     if (!GetRectangle(pszResult, uiResultSize, uiNargs, ppArgs, &rc)) {
  105.         return FALSE;
  106.     }
  107.  
  108.     Status("Ellipse(%d,%d,%d,%d)", rc.left, rc.top, rc.right, rc.bottom);
  109.  
  110.     hDC = GetDC(hwndMain);
  111.  
  112.     SetTools(hDC);
  113.     Ellipse(hDC, rc.left, rc.top, rc.right, rc.bottom);
  114.     RestoreTools(hDC);
  115.  
  116.     ReleaseDC(hwndMain, hDC);
  117.  
  118.     return TRUE;
  119. }
  120.  
  121. //
  122. // Rectangle(left,top,right,bottom)
  123. //
  124.  
  125. BOOL FAR RectangleFn(PDDETOPICINFO pTopic,
  126.                  LPSTR pszResult,
  127.                  UINT uiResultSize,
  128.                  UINT uiNargs,
  129.                  LPSTR FAR *ppArgs)
  130. {
  131.     HDC hDC;
  132.     RECT rc;
  133.  
  134.     //
  135.     // Get the co-ordinates
  136.     //
  137.  
  138.     if (!GetRectangle(pszResult, uiResultSize, uiNargs, ppArgs, &rc)) {
  139.         return FALSE;
  140.     }
  141.  
  142.     Status("Rectangle(%d,%d,%d,%d)", rc.left, rc.top, rc.right, rc.bottom);
  143.  
  144.     hDC = GetDC(hwndMain);
  145.  
  146.     SetTools(hDC);
  147.     Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
  148.     RestoreTools(hDC);
  149.  
  150.     ReleaseDC(hwndMain, hDC);
  151.  
  152.     return TRUE;
  153. }
  154.  
  155. //
  156. // Line(left,top,right,bottom)
  157. //
  158.  
  159. BOOL FAR LineFn(PDDETOPICINFO pTopic,
  160.                  LPSTR pszResult,
  161.                  UINT uiResultSize,
  162.                  UINT uiNargs,
  163.                  LPSTR FAR *ppArgs)
  164. {
  165.     HDC hDC;
  166.     RECT rc;
  167.  
  168.     //
  169.     // Get the co-ordinates
  170.     //
  171.  
  172.     if (!GetRectangle(pszResult, uiResultSize, uiNargs, ppArgs, &rc)) {
  173.         return FALSE;
  174.     }
  175.  
  176.     Status("Line(%d,%d,%d,%d)", rc.left, rc.top, rc.right, rc.bottom);
  177.  
  178.     hDC = GetDC(hwndMain);
  179.  
  180.     SetTools(hDC);
  181.     MoveTo(hDC, rc.left, rc.top);
  182.     LineTo(hDC, rc.right, rc.bottom);
  183.     RestoreTools(hDC);
  184.  
  185.     ReleaseDC(hwndMain, hDC);
  186.  
  187.     return TRUE;
  188. }
  189.  
  190. //
  191. // Erase()
  192. //
  193.  
  194. BOOL FAR EraseFn(PDDETOPICINFO pTopic,
  195.                  LPSTR pszResult,
  196.                  UINT uiResultSize,
  197.                  UINT uiNargs,
  198.                  LPSTR FAR *ppArgs)
  199. {
  200.  
  201.     Status("Erase()");
  202.  
  203.     SetPenColor(RGB(0,0,0));
  204.     SetBrushColor(RGB(255,255,255));
  205.     InvalidateRect(hwndMain, NULL, TRUE);
  206.  
  207.     return TRUE;
  208. }
  209.  
  210. //
  211. // Text(left,top,text)
  212. //
  213.  
  214. BOOL FAR TextFn(PDDETOPICINFO pTopic,
  215.                  LPSTR pszResult,
  216.                  UINT uiResultSize,
  217.                  UINT uiNargs,
  218.                  LPSTR FAR *ppArgs)
  219. {
  220.     HDC hDC;
  221.     int x, y;
  222.     char buf[32];
  223.  
  224.     //
  225.     // Get the co-ordinates
  226.     //
  227.  
  228.     _fstrncpy(buf, ppArgs[0], sizeof(buf)-1);
  229.     x = atoi(buf);
  230.     _fstrncpy(buf, ppArgs[1], sizeof(buf)-1);
  231.     y = atoi(buf);
  232.  
  233.     hDC = GetDC(hwndMain);
  234.  
  235.     SetTools(hDC);
  236.     TextOut(hDC, x, y, ppArgs[2], _fstrlen(ppArgs[2]));
  237.     RestoreTools(hDC);
  238.  
  239.     ReleaseDC(hwndMain, hDC);
  240.  
  241.     Status("Text(%d,%d,%s)", x, y, ppArgs[2]);
  242.  
  243.     return TRUE;
  244. }
  245.  
  246. //
  247. // Get rectangle co-ordinates
  248. //
  249.  
  250. static BOOL GetRectangle(LPSTR pszError,
  251.                  UINT uiErrorSize,
  252.                  UINT uiNargs,
  253.                  LPSTR FAR *ppArgs,
  254.                  LPRECT pRect)
  255. {
  256.     char buf[32];
  257.  
  258.     if (uiNargs != 4) {
  259.         _fstrncpy(pszError,
  260.                   "Need 4 args",
  261.                   uiErrorSize-1);
  262.         return FALSE;
  263.     }
  264.  
  265.     _fstrncpy(buf, ppArgs[0], sizeof(buf)-1);
  266.     pRect->left = atoi(buf);
  267.  
  268.     _fstrncpy(buf, ppArgs[1], sizeof(buf)-1);
  269.     pRect->top = atoi(buf);
  270.  
  271.     _fstrncpy(buf, ppArgs[2], sizeof(buf)-1);
  272.     pRect->right = atoi(buf);
  273.  
  274.     _fstrncpy(buf, ppArgs[3], sizeof(buf)-1);
  275.     pRect->bottom = atoi(buf);
  276.  
  277.     return TRUE;
  278. }
  279.  
  280. //
  281. // Select the drawing tools to use
  282. //
  283.  
  284. static void SetTools(HDC hDC)
  285. {
  286.     if (!hpenCurrent) {
  287.         rgbPen = RGB(0,0,0);
  288.         hpenCurrent = CreatePen(PS_SOLID, 1, rgbPen);
  289.     }
  290.     hpenOld = SelectObject(hDC, hpenCurrent);
  291.     SetTextColor(hDC, rgbPen);
  292.  
  293.     if (!hbrCurrent) {
  294.         rgbBrush = RGB(128,128,128);
  295.         hbrCurrent = CreateSolidBrush(rgbBrush);
  296.     }
  297.     hbrOld = SelectObject(hDC, hbrCurrent);
  298.     SetBkColor(hDC, rgbBrush);
  299. }
  300.  
  301. //
  302. // Restore the tools
  303. //
  304.  
  305. static void RestoreTools(HDC hDC)
  306. {
  307.     SelectObject(hDC, hpenOld);
  308.     SelectObject(hDC, hbrOld);
  309. }
  310.  
  311. //
  312. // Get a color argument
  313. //
  314.  
  315. static BOOL GetColor(LPSTR pszError,
  316.                  UINT uiErrorSize,
  317.                  UINT uiNargs,
  318.                  LPSTR FAR *ppArgs,
  319.                  COLORREF *pColor)
  320. {
  321.     if (uiNargs != 1) {
  322.         Status("Invalid number of args");
  323.         _fstrncpy(pszError,
  324.                   "Need 1 args",
  325.                   uiErrorSize-1);
  326.         return FALSE;
  327.     }
  328.  
  329.     if (_fstricmp(ppArgs[0], "RED") == 0) {
  330.         *pColor = RGB(255,0,0);
  331.     } else if (_fstricmp(ppArgs[0], "GREEN") == 0) {
  332.         *pColor = RGB(0,255,0);
  333.     } else if (_fstricmp(ppArgs[0], "BLUE") == 0) {
  334.         *pColor = RGB(0,0,255);
  335.     } else if (_fstricmp(ppArgs[0], "BLACK") == 0) {
  336.         *pColor = RGB(0,0,0);
  337.     } else if (_fstricmp(ppArgs[0], "WHITE") == 0) {
  338.         *pColor = RGB(255,255,255);
  339.     } else if (_fstricmp(ppArgs[0], "GRAY") == 0) {
  340.         *pColor = RGB(127,127,127);
  341.     } else if (_fstricmp(ppArgs[0], "CYAN") == 0) {
  342.         *pColor = RGB(0,255,255);
  343.     } else if (_fstricmp(ppArgs[0], "YELLOW") == 0) {
  344.         *pColor = RGB(255,255,0);
  345.     } else if (_fstricmp(ppArgs[0], "MAGENTA") == 0) {
  346.         *pColor = RGB(255,0,255);
  347.     } else {
  348.         Status("Invalid color");
  349.         _fstrncpy(pszError,
  350.                   "Invalid color",
  351.                   uiErrorSize-1);
  352.         return FALSE;
  353.     }
  354.  
  355.     return TRUE;
  356. }
  357.  
  358. //
  359. // Set the current pen
  360. //
  361.  
  362. static void SetPenColor(COLORREF color)
  363. {
  364.     if (hpenCurrent) {
  365.         DeleteObject(hpenCurrent);
  366.     }
  367.     hpenCurrent = CreatePen(PS_SOLID, 1, color);
  368.     rgbPen = color;
  369. }
  370.  
  371. //
  372. // Set the current brush
  373. //
  374.  
  375. static void SetBrushColor(COLORREF color)
  376. {
  377.     if (hbrCurrent) {
  378.         DeleteObject(hbrCurrent);
  379.     }
  380.     hbrCurrent = CreateSolidBrush(color);
  381.     rgbBrush = color;
  382. }
  383.